// pages/teamNews/teamNews.js
const app = getApp();
const {
  teamNews,
  dealInvite
} = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.appRequest({
      url: teamNews,
      success: (res)=>{
        // console.log(res);
        let merchant = res.data.merchant;
        if (res.data.invite.length > 0) {
          let dataList = res.data.invite.map((item) => {
            if (merchant[item.merchantId]) {
              item.name = merchant[item.merchantId].name;
            }
            return item
          })
          this.setData({
            dataList,
            nolistTip: 0
          })
        }else{
          this.setData({
            nolistTip: -1
          })
        }
        
      },
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  dealInvite: function(e){
    console.log(e);
    let name = e.currentTarget.dataset.name;
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: `提示`,
      cancelText: `不同意`,
      confirmText: `同意`,
      content: `是否加入${name}`,
      confirmColor: '#1E66CC',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          app.appRequest({
            url: dealInvite,
            data: {
              type:1,
              id: id,
            },
            method: 'POST',
            success: (res) => {
              wx.navigateBack({
                delta: 1,
              })
            },
          })
        } else if (res.cancel) {
          app.appRequest({
            url: dealInvite,
            data: {
              type: 0,
              id: id,
            },
            method: 'POST',
            success: (res) => {
              wx.navigateBack({
                delta: 1,
              })
            },
          })
        }
      }
    })
  }
})